package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetMealServiceImpl implements SetMealService {
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;


    // 分页查询
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> p=setmealMapper.pageQuery(setmealPageQueryDTO);
       // Page<SetmealVO> p= (Page<SetmealVO>) list;
        return new PageResult(p.getTotal(),p.getResult());
    }


    // 新增套餐
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.save(setmeal);
        //获取当前套餐的id
        Long id = setmeal.getId();
        //添加套餐和菜品的关联关系
        List<SetmealDish> list = setmealDTO.getSetmealDishes();
        if (list!=null && list.size()>0){
            for (SetmealDish setmealDish : list) {
                setmealDish.setSetmealId(id);
            }
            setmealDishMapper.save(list);
        }
    }
    //删除套餐
    @Override
    public void delete(List<Long> ids) {
        // 判断套餐是否在起售中
        for (Long id : ids) {
            Integer status=setmealMapper.getStatusBiId(id);
            if(status==StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐
        setmealMapper.delete(ids);
        //删除套餐和菜品的关联信息删除套餐和菜品的关联关系
        for (Long id : ids) {
            setmealDishMapper.deleteBySetmealId(id);
        }


    }

    //查询回显->根据id查询套餐
    @Override
    public SetmealVO getSetmealById(Long id) {
        //查询套餐基本信息
        SetmealVO setmealVO= setmealMapper.getSetmealById(id);
        //查询套餐和菜品的关联信息
       List<SetmealDish> list= setmealDishMapper.getSetmealDishBySetmealId(id);
         setmealVO.setSetmealDishes(list);
         return setmealVO;

    }

    //修改套餐
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐基本信息
        setmealMapper.updateSetmeal(setmeal);
        //删除原有套餐和菜品的关联信息
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        //新增新的套餐和菜品的关联信息
        setmealDishMapper.save(setmealDTO.getSetmealDishes());

    }

    // 起售停售
    @Override
    public void startOrStop(Integer status, long id) {
        Setmeal setmeal=Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.updateSetmeal(setmeal);
    }


    //根据分类id查询套餐
    @Override
    public List<Setmeal> getSetmealByCategoryId(Setmeal setmeal) {
        return setmealMapper.getSetmealByCategoryId(setmeal);
    }

    //根据套餐id查询菜品选项
    @Override
    public List<DishItemVO> getByIdWithDish(Long setmealId) {
       List<DishItemVO> list= setmealDishMapper.getDishItmBySetmealId(setmealId);
        return list;
    }

}
